var docStyle = "default";

function setDocStyle(ds) {
	docStyle = ds;
	document.getElementById("customedCss").href = "/Photostock/css/" + ds + ".css";
}		   

function initAjax() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else return false;
}

function ajaxIt(element, action, params) {
	var ajaxRequest = initAjax();
	if (ajaxRequest) {
		var url = "AjaxServlet?action="+action+"&" + params;
		ajaxRequest.open("POST", url, true);
		ajaxRequest.onreadystatechange = function() {
			if (ajaxRequest.readyState == 4) {
				treatResponse(element, ajaxRequest.responseText);
			}
		}
	}
	ajaxRequest.send(null);

}

function setError(errName, errText) {
	var errField = document.getElementById(errName);
	errField.style.display="block";
	errField.innerHTML = errText;
}

function treatResponse(element, response) {
	if (response) {
		if (response.indexOf("|") == -1) {
			subItems = response.split("##");
			document.getElementById(subItems[0]).innerHTML = subItems[1];
			document.body.style.backgroundImage = "url('/Photostock/images/site/"+docStyle+"/fond.png')";
		} else {
			var items = response.split("|");
			if (items[0] == "nok") {
				if (element.tagName != "FORM") {
					element.value = "";
					element.focus();
					element.form.Valider.disabled=true;
				}
				alert(items[1]);
			} else if (items[0] != "ok") {
				for (var i=0; i<items.length; i++) {
					subItems = items[i].split("##");
					if ((subItems[0] != "head") && (subItems[0] != "style")) {
						document.getElementById(subItems[0]).innerHTML = subItems[1];
					} else if (subItems[0] == "style") {
						docStyle = subItems[1];
						document.getElementById("customedCss").href = "/Photostock/css/" + subItems[1] + ".css";
					}
				}
				document.body.style.backgroundImage = "url('/Photostock/images/site/"+docStyle+"/fond.png')";
			} else if (items[0] == "ok") {
				element.form.Valider.disabled=false;
				if (element.type == "button")
					document.body.style.backgroundImage = "url('/Photostock/images/site/"+docStyle+"/fond.png')";
			}
			
		}
	}
}

